require("../css/person.less")
document.ready(function () {
    //获取dom
    let showDateGender = document.querySelector("#showDateGender")
    let showDateBirthday = document.querySelector("#showDateBirthday")
    let showDateProvince = document.querySelector("#showDateProvince")
    let showDateCity = document.querySelector("#showDateCity")
    let gender = document.querySelector("#gender")//性别
    let birthday = document.querySelector("#birthday")//生日
    let province = document.querySelector("#province")//省份
    let city = document.querySelector("#city")//城市
    let signTxt = document.querySelector("#signTxt")//签名
    let signNum = document.querySelector("#signNum")//签名个数
    let back = document.querySelector("#back")//返回
    let nickName = document.querySelector("#nickName")//昵称
    let saveBtn = document.querySelector("#saveBtn")//保存

    let provinceAll = [
        // {
        //     label: '湖北省',
        //     value: 0
        // }, {
        //     label: '湖南省',
        //     value: 1
        // },
    ]
    let cityAll = [
        // {
        //     label: '湖北省',
        //     value: 0
        // }, {
        //     label: '湖南省',
        //     value: 1
        // },
    ]
    // 发送请求获取省份信息
    axios.get(window.$utils.url + "/address/province")
        .then(function (res) {
            let ret = res.data;
            // console.log(ret.data);
            if (ret.status == 0) {
                provinceAll = ret.data.map(function (p) {
                    let obj =
                    {
                        label: p.name,
                        value: p.addressId
                    }
                    return obj
                })
            }
        })
        .catch(function (error) {
            console.log(error);
        })

    //性别
    showDateGender.addEventListener("click", function () {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            value: 1
        },], {
            onConfirm: function (result) {
                // console.log(result[0]);
                gender.textContent = result[0].label
            },
            title: '请选择性别'
        });
    })
    // 生日
    showDateBirthday.addEventListener("click", function () {
        weui.datePicker({
            start: 1990,
            end: new Date().getFullYear(),
            onConfirm: function (result) {
                // console.log(result);
                let str = result.map(function (b) {
                    return window.$utils.addZero(b.value);
                }).join("-")
                birthday.textContent = str
            },
            title: '请选择您的生日'
        });
    })

    //省份
    showDateProvince.addEventListener("click", function () {
        weui.picker(provinceAll, {
            //选择省份
            onConfirm: function (result) {
                console.log(result);
                province.textContent = result[0].label
                //拿当前省份的id去获取对应的市区
                axios.get(window.$utils.url + `/address/city/${result[0].value}`)
                    .then(function (res) {
                        let ret = res.data
                        if (ret.status == 0) {
                            //遍历数组组装城市的数组
                            cityAll = ret.data.map(function (c) {
                                let obj =
                                {
                                    label: c.name,
                                    value: c.addressId
                                }
                                return obj
                            })
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    })
            },
            title: '请选择省份'
        });
    })
    //城市
    showDateCity.addEventListener("click", function () {
        if (cityAll.length > 0) {
            weui.picker(cityAll, {
                onConfirm: function (result) {
                    console.log(result);
                    city.textContent = result[0].label
                },
                title: '请选择城市'
            });
        }
    })

    // 签名
    signTxt.addEventListener("input", function () {
        signNum.textContent = this.value.length
    })


    // 返回上一级
    back.addEventListener("click", function () {
        history.go(-1)
    })
    // 从本地拿取信息做显示
    let user = JSON.parse(localStorage.getItem("user"))
    // console.log(user);
    nickName.value = user.nickname
    gender.textContent = user.gender ? user.gender : "未选择"
    birthday.textContent = user.birthday ? window.$utils.formatDate(user.birthday) : "未选择"
    province.textContent = user.address ? user.address.split(",")[0] : "未选择"
    city.textContent = user.address ? user.address.split(",")[1] : "未选择"
    signTxt.textContent = user.sign

    // 保存
    saveBtn.addEventListener("click", function () {
        axios.post(window.$utils.url + "/users/userEdit", {
            "nickname": nickName.value,
            "gender": gender.textContent,
            "birthday": birthday.textContent,
            "address": [province.textContent, city.textContent],
            "sign": signTxt.value,
            "userId": user.userId,
        })
            .then(function (res) {
                let ret = res.data;
                if (ret.status == 0) {
                    window.$utils.toast("success", "修改成功!")
                    setTimeout(function () {
                        location.href = "./mine.html"
                    }, 3000)
                }
            })
            .catch(function (error) {
                console.log(error);
            })
    })
})
